<?php

namespace api\modules\v1\forms;

use yii\base\Model;
use common\helpers\RegularHelper;
use common\models\member\Member;
use common\models\api\AccessToken;
use common\models\common\SmsLog;
use common\models\validators\SmsCodeValidator;

/**
 * Class RegisterForm
 * @package api\modules\v1\forms
 * @author  jianyan74 <751393839@qq.com>
 */
class RegisterForm extends Model
{
    public $mobile;
    public $password;
    public $password_repetition;
    public $code;
    public $group;
    //    public $realname; `
    public $username;
    public $pid;
    public $email;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['group', 'password', 'password_repetition', 'pid'], 'required'],
            [['username'], 'string'],
            [['password'], 'string', 'min' => 6],
            [
                ['mobile'],
                'unique',
                'targetClass'     => Member::class,
                'targetAttribute' => 'mobile',
                'message'         => '此手机号已注册。',
            ],
            ['code', 'validateCode'],
            //            ['mobile', 'match', 'pattern' => RegularHelper::mobile(), 'message' => '请输入正确的手机号码'],
            ['mobile', 'validateMobile'],
            [['password_repetition'], 'compare', 'compareAttribute' => 'password'],
            // 验证新密码和重复密码是否相等
            ['group', 'in', 'range' => AccessToken::$ruleGroupRnage],
        ];
    }

    /**
     * 验证手机号格式
     *
     * @param $attribute
     * @param $params
     */
    public function validateMobile($attribute, $params)
    {
        if (empty($this->mobile)) {
            $this->addError($attribute, '手机号码或邮箱不能为空');
        }
        if (strpos($this->mobile, "@")) {
            //邮箱
            if (!RegularHelper::verify('email', $this->mobile)) {
                $this->addError($attribute, '邮箱格式有误');
            }
        }else {
            //手机
            if (!RegularHelper::verify('mobile', $this->mobile)) {
                $this->addError($attribute, '手机号码格式有误');
            }
        }
    }


    public function validateCode($attribute, $params)
    {
        $expireTime = 60 * 15;
        //查询短信验证码日志
        $smsLog = SmsLog::find()
                        ->where([
                            'mobile'     => $this->mobile,
                            'error_code' => 200,
                            'used'       => 0,
                        ])
                        ->orderBy('id desc')
                        ->one();
        /** @var $smsLog SmsLog */
        $time = time();
        if (is_null($smsLog) || ($smsLog->code != $this->code) || ($smsLog->created_at > $time || $time > ($smsLog->created_at + $expireTime))) {
            $this->addError($attribute, '验证码错误');
        }else {
            $smsLog->used     = 1;
            $smsLog->use_time = $time;
            $smsLog->save();
        }
    }

    public function attributeLabels()
    {
        return [
            'mobile'              => '手机号码或邮箱',
            'password'            => '密码',
            'password_repetition' => '重复密码',
            'group'               => '类型',
            'code'                => '验证码',
            'username'            => '用户名',
            'pid'                 => '邀请人',
            'email'               => '邮箱',
        ];
    }
}